package com.pinganfu.tesla.iwallet;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.pinganfu.tesla.iwallet.bean.BankCard;
import com.pinganfu.tesla.iwallet.util.LogUtil;
import com.pinganfu.tesla.iwallet.util.TeslaUtil;
import compinganfu.tesla.iwallet.widget.BaseActivity;

public class CardListActivity extends BaseActivity implements OnClickListener {

	private LayoutInflater inflater = null;
	private LinearLayout layoutCCList = null;
	private LinearLayout layoutDCList = null;
	private Button btnAddCard = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_card_list);

		inflater = LayoutInflater.from(this);
		layoutCCList = (LinearLayout) findViewById(R.id.layout_credit_card_list);
		layoutDCList = (LinearLayout) findViewById(R.id.layout_debit_card_list);

		btnAddCard = (Button) findViewById(R.id.btn_add_card);
		btnAddCard.setOnClickListener(this);

		initCardList();
	}

	private void initCardList() {
		List<BankCard> creditCards = new ArrayList<BankCard>();
		for (int i = 0; i < 2; i++) {
			BankCard card = new BankCard("工商银行", String.valueOf(1000 + i), "C");
			creditCards.add(card);
		}
		initCards(creditCards, layoutCCList);

		List<BankCard> debitCards = new ArrayList<BankCard>();
		for (int i = 0; i < 6; i++) {
			BankCard card = new BankCard("招商银行", String.valueOf(2000 + i), "D");
			debitCards.add(card);
		}
		initCards(debitCards, layoutDCList);
	}

	private void initCards(List<BankCard> cards, final LinearLayout layout) {
		int cardIndex = 0;
		int cardCount = cards.size();

		for (BankCard bc : cards) {
			LinearLayout view = (LinearLayout) inflater.inflate(R.layout.card_item, null);
			LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 160);
			view.setLayoutParams(params);
			view.setGravity(Gravity.CENTER_VERTICAL);

			((TextView) view.findViewById(R.id.tv_bank_name)).setText(bc.getBankName());
			((TextView) view.findViewById(R.id.tv_card_no)).setText("尾号" + bc.getCardNo());

			view.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					CharSequence cardNo = ((TextView) v.findViewById(R.id.tv_card_no)).getText();
					LogUtil.i("cardNo: " + cardNo);

					// TeslaUtil.gotoActivity(HomeActivity.this,
					// CardActivity.class);
				}
			});

			view.setOnLongClickListener(new OnLongClickListener() {

				@Override
				public boolean onLongClick(View v) {
					CharSequence cardNo = ((TextView) v.findViewById(R.id.tv_card_no)).getText();

					showConfirmDialog(layout, v, cardNo);
					return true;
				}
			});

			layout.addView(view);

			if (cardIndex + 1 >= cardCount) {
				View divider = view.findViewById(R.id.tv_divider);
				view.removeView(divider);
			}
			cardIndex++;
		}
	}

	protected void showConfirmDialog(final LinearLayout layout, final View view, final CharSequence cardNo) {
		AlertDialog.Builder alert = new AlertDialog.Builder(this).setMessage("确定解绑银行卡（" + cardNo + "）吗？").setTitle(R.string.lbl_tips);
		alert.setCancelable(true);
		alert.setNegativeButton(R.string.lbl_cancel, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}

		});
		alert.setPositiveButton(R.string.lbl_confirm, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Intent intent = new Intent(CardListActivity.this, UnbindCardActivity.class);
				intent.putExtra("cardNo", cardNo);
				CardListActivity.this.startActivity(intent);
			}
		});
		alert.show();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.btn_add_card :
				TeslaUtil.gotoActivity(this, AddCardActivity.class);
				break;
		}
	}
}
